package com.fone.player.activity.personal.adapter;

import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.entity.OfflineCache;

/**
 * 缓存电视剧选择adapter。
 * 
 * @author JasonZue
 * @since 2014-4-22
 */
public class CacheGridAdatper extends BaseAdapter {

	private List<OfflineCache> gridData;
	private Context mContext;
	@SuppressLint("UseSparseArrays")
	private HashMap<Integer, String> selectedPosition = new HashMap<Integer, String>();

	/**
	 * @param mContext
	 * @param collectionData
	 *            传入条目数据List
	 * @param autoDownload
	 *            传入记录选中下载的list对象
	 */
	public CacheGridAdatper(Context mContext, List<OfflineCache> collectionData) {
		this.mContext = mContext;
		this.gridData = collectionData;
	}

	@Override
	public int getCount() {
		return gridData.size();
	}

	public void setSeclection(int position) {

		if (selectedPosition.containsKey(position)) {
			selectedPosition.remove(position);
		} else {
			selectedPosition.put(position, "");
		}
	}

	public int getSelectedCount() {

		return selectedPosition.size();

	}

	public HashMap<Integer, String> getSeletedMap() {
		return selectedPosition;
	}

	public void cancleSeleted() {
		selectedPosition.clear();
	}

	@Override
	public Object getItem(int position) {
		return gridData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext,
					R.layout.personal_cache_grid_item, null);

			holder.itemNUM = (TextView) convertView
					.findViewById(R.id.cache_grid_tvnum_tv);
			holder.itemStatusIv = (ImageView) convertView
					.findViewById(R.id.cache_grid_status_iv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		String num = gridData.get(position).getCacheEpisodeNum();

		num.substring(num.indexOf("第") + 1, num.indexOf("集"));

		holder.itemNUM.setText(num.substring(num.indexOf("第") + 1, num.indexOf("集")));
		if (null != selectedPosition.get(position)) {
			// L.v("JasonZue", "执行了");
			holder.itemStatusIv.setVisibility(View.VISIBLE);
			holder.itemStatusIv
					.setImageResource(R.drawable.detail_download_choose);
			holder.itemNUM.setTextColor(mContext.getResources().getColor(
					R.color.cache_page_text_orange_color));
		} else {
			// L.v("JasonZue", "执行了else:position:" + position);
			switch (gridData.get(position).getCacheDownloadState()) {
			case OfflineCache.CACHE_STATE_DOWNLOADING:
			case OfflineCache.CACHE_STATE_QWAITING:
			case OfflineCache.CACHE_STATE_WAITING:
			case OfflineCache.CACHE_STATE_ERROR:
			case OfflineCache.CACHE_STATE_PAUSE:
			case OfflineCache.CACHE_STATE_PAUSE_USER:
				holder.itemStatusIv.setVisibility(View.VISIBLE);
				holder.itemStatusIv
						.setImageResource(R.drawable.detail_download_finished_un);
				holder.itemNUM.setTextColor(mContext.getResources().getColor(
						R.color.black));
				break;
			case OfflineCache.CACHE_STATE_FINISH:
				holder.itemStatusIv.setVisibility(View.VISIBLE);
				holder.itemStatusIv
						.setImageResource(R.drawable.detail_download_finished);
				holder.itemNUM.setTextColor(mContext.getResources().getColor(
						R.color.cache_item_green_color));
				break;

			case OfflineCache.CACHE_STATE_NOT_DOWNLOAD:
				holder.itemStatusIv.setVisibility(View.GONE);
				holder.itemStatusIv
						.setImageResource(R.drawable.detail_download_finished_un);
				holder.itemNUM.setTextColor(mContext.getResources().getColor(
						R.color.black));
				break;
			default:
				holder.itemStatusIv.setVisibility(View.GONE);
				holder.itemStatusIv
						.setImageResource(R.drawable.detail_download_finished_un);
				holder.itemNUM.setTextColor(mContext.getResources().getColor(
						R.color.black));
				break;
			}
		}

		return convertView;

	}

	class ViewHolder {
		public TextView itemNUM;
		public ImageView itemStatusIv;

	}

}
